<?php
$cdataProvider=new CActiveDataProvider('Payments', array(
			'criteria'=>array(
			'condition'=>'idOccupancy='.$oid.' AND idIncomeType IN (select id from Incometype where Category in( "Payment" , "Fee"))',
			   
			),
			'pagination'=>array(
				'pageSize'=>5,
			),
		));
	
		
   function getCurrentBalance($cdataProvider) {
	   foreach($cdataProvider->data as $item)
	    { 
	    	$_minus = array('Returned','Expense','Fee');
			    	//echo in_array($item->idIncomeType0->Name, $_minus);
			    	if(in_array($item->idIncomeType0->Name, $_minus) || in_array($item->idIncomeType0->Category, $_minus))
			    	$total-=$item->Amount;
			    	else
			    	$total += $item->Amount;

	    }
	    return  $total;
   }
    function getSum($cdataProvider,$cat) {
    	$total=0;
	   foreach($cdataProvider->data as $item)
	    { 
	    	
	    	if($item->idIncomeType0->Category == $cat)
			    	$total += $item->Amount;

	    }
	    return  $total;
   }
   class DueColumn extends CGridColumn {
		 
		    private  $_total = 0;
		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		    	
		    if($data->idIncomeType0->Category == 'Fee')
		        echo $data->Amount;
		        
		    }
		  
		}
		
		class PaidColumn extends CGridColumn {
		 
		    private  $_total = 0;
		   	
		   public function renderDataCellContent($row, $data) { // $row number is ignored
		  		    	
		    if($data->idIncomeType0->Category != 'Fee')
		        echo $data->Amount;
		        
		    }
		  
		}	
 $dueTotal=getSum($cdataProvider, 'Fee');
$total=getCurrentBalance($cdataProvider);
	$paidTotal=getSum($cdataProvider, 'Payment');	
?>



<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'paymentsfee-grid',
	'dataProvider'=>$cdataProvider,
	
	'columns'=>array(
		array(            
            'name'=> 'PaidDate',
			'header'=>'Date',
		
		
        ),
        array(            
            'name'=> 'idIncomeType',
			'header'=>'Type',
			'type'=>'raw',
			'value'=>'$data->idIncomeType0->Name ',
        ),
		array(            
            'header'=> 'Due',
			'class'=>'DueColumn',
			'footer' => $dueTotal,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
        ),
        array(            
            'header'=> 'Paid',
			'class'=>'PaidColumn',
			'footer' => $paidTotal,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
        ),
		
		
        array(
            'header' => 'Balance',
            'class'  => 'BalanceColumn',
        	'footer' => $total,
        	'footerHtmlOptions' => array('class'=>'tblfooter'),
            
        ),
	  
		array(
			'class'=>'CButtonColumn',
    'template'=>'{view} {update} {delete}',
    'buttons'=>array
    (
        'view' => array
        (
            'label'=>'View Detail',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/view.png',
            'url'=>'Yii::app()->createUrl("/adminpanel/payments/view", array("id"=>$data->id,"typ"=>"Security Deposits", "rurl"=>Yii::app()->request->requestUri))',
        ),
        'update' => array
        (
            'label'=>'Update Detail',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/update.png',
            'url'=>'Yii::app()->createUrl("adminpanel/payments/update", array("id"=>$data->id,"typ"=>"Security Deposits" , "rurl"=>Yii::app()->request->requestUri))',
            
        ),
		
		'delete' => array
        (
            'label'=>'Delete',
            'imageUrl'=>Yii::app()->request->baseUrl.'/assets/a99e585a/gridview/delete.png',
            'url'=>'Yii::app()->createUrl("adminpanel/payments/delete", array("id"=>$data->id))',
            
        ),
		
    ),
		),
		
	),
	
)); 


?>
